<template>
  <div class="btns">
    <Button v-auth="'secondEntry'" color="success" @click="entry" v-if="freshGraduate">入职</Button>
    <Button v-auth="'accomplishSign'"  color="primary" @click="finishSign" v-else>完成签约</Button>
    <Button v-auth="'secondEliminate'"  color="error" @click="dieout">淘汰</Button>
    <Button v-auth="'secondWaive'"  color="warning" @click="surrender">放弃</Button>
  </div>
  <Modal ref="modal"></Modal>

  <BasicModal :show="dieOutShow" title="淘汰" width="30%" :widthComfirm="true" @closeModal="dieOutShow = false"
    @comfirm="dieOutShowComfirm">
    <template #content>
      <CommonForm :formList="formList" ref="formRef"></CommonForm>
    </template>
  </BasicModal>

  <BasicModal :show="show" title="上传三方协议照片" width="30%" :widthComfirm="true" @comfirm="comfirm"
    @closeModal="show = false">
    <template #content>
      <a-upload v-model:file-list="fileList" list-type="picture-card" action="#" accept=".png,.jpg" multiple
        :before-upload="beforeUpload" @preview="handlePreview">
        <div v-if="fileList&&fileList.length < 4" class="uploader">
          <i class="iconfont icon-shangchuan"></i>
          <div style="margin-top: 8px">上传图片</div>
        </div>
      </a-upload>
    </template>
  </BasicModal>
  <a-modal :open="previewVisible" :title="previewTitle" :footer="null" @cancel="handleCancel">
    <img alt="example" style="width: 100%" :src="previewImage" />
  </a-modal>

</template>

<script setup lang='ts'>
import { Button } from "@/components/Button";
import Modal from "../../../../Modals/Modal.vue";
import BasicModal from "@/views/ApplicationDetails/Modals/BasicModal.vue";
import type { UploadProps } from 'ant-design-vue';
import { message } from "ant-design-vue";
import {
  secondInterview, uploadContractFile,contractSigned
} from "@/api/application-details/index.ts";
import { comStores } from '@/store/common/comStore';
import type { IndexFormItem } from "@/views/ApplicationDetails/application-type.ts";
import CommonForm from "@/views/applicationDetails/CommonForm.vue";
const store = comStores();
const show = ref<boolean>(false);
const dieOutShow = ref<boolean>(false)
// const { resumeId, processId } = store.currentResume;
const { userName } = store.userInFor;
const fileList = ref<any>([]);
const previewVisible = ref<boolean>(false);

const previewImage = ref<string>('');
const previewTitle = ref<string>('');
// store.getResumeInfo(resumeId)
import { ref } from "vue"
const emits = defineEmits(['surrender', 'dieOut'])
const freshGraduate = ref<boolean>(true)
const modal = ref<any>(null)
const surrender = () => {
  console.log('surrender')
  emits('surrender', '二次面试')
}
const entry = () => {
  console.log('二次面试', 'entry')
  modal.value.showDialog("EntryModal", '二次面试')
}

const finishSign = () => {
  console.log('二次面试', 'finishSign')
  show.value = true
}

const formList = ref<IndexFormItem[]>([
  { label: "淘汰原因", name: "remark", value: "", component: 'textarea', required: false },
])
const dieout = () => {
  // emits('dieOut', '二次面试')
  console.log('二次面试', 'dieout')
  dieOutShow.value = true
}
const beforeUpload = (fileList) => {
  return false;
}
const handleCancel = () => {
  previewVisible.value = false;
  previewTitle.value = '';
};
function getBase64(file: File) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
  });
}
const handlePreview = async (file: UploadProps['fileList'][number]) => {
  if (!file.url && !file.preview) {
    file.preview = (await getBase64(file.originFileObj)) as string;
  }
  previewImage.value = file.url || file.preview;
  previewVisible.value = true;
  previewTitle.value = file.name || file.url.substring(file.url.lastIndexOf('/') + 1);
};

const comfirm = async () => {
  console.log(fileList.value);
  const formdata = new FormData()
  const { resumeId, processId } = store.currentResume;
  if (fileList.value.length > 0) {
    fileList.value?.forEach((el, idx) => {
      const file = el.originFileObj as File;
      formdata.append('files', file);
    })
    formdata.append('resumeId', resumeId)
    const res = await uploadContractFile(formdata);
    if (res && res.code === '10000') {
      message.success('上传协议照片成功');
    } else {
      message.error(res.msg);
      return
    }
  }
  let param = {
    resumeId: resumeId,
    processId: processId,
    result: "通过",
    operator: userName,
  }
  const data = await contractSigned(param)
  if (data && data.code === '10000') {
    message.success("操作成功");
    show.value = false
    await store.getResumeInfo(resumeId)
  } else {
    message.error(data.msg)
  }
}
const formRef = ref<any>(null);
const dieOutShowComfirm = async () => {
  const res = await formRef.value.submitForm();
  if (!res) return
  const { resumeId, processId } = store.currentResume;
  let param = {
    resumeId: resumeId,
    processId: processId,
    operator: userName,
    editStatus: "PASS",
    reason: res[0].value,
  }
  const data = await secondInterview(param)
  if (data && data.code === '10000') {
    message.success("操作成功")
    await store.getResumeInfo(resumeId)
  } else {
    message.error(data.msg)
  }
}
</script>
<style lang="less" scoped>
.btns {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}
</style>
